package hhm.android.library

import android.content.Context
import android.util.TypedValue
import android.view.View
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import hhm.android.library.calendar.view.CalendarView

class BottomDataAnalyseDateSelectSheet(
    context: Context,
    private var list: ArrayList<Long>,
    private var listener: GetDateBeanListener
) : BottomSheetDialog(
    context
) {

    init {
        createView()
    }

    private fun createView() {
        val bottomSheetView: View =
            layoutInflater.inflate(R.layout.item_bottom_data_analyse_date_select, null)
        setContentView(bottomSheetView)

        val calendar = bottomSheetView.findViewById<CalendarView>(R.id.calendar)
        calendar.init(list)

        calendar.setDateClick { dateBean, _, _ ->
            if (dateBean.hasData) {
                calendar.updateSelectDay(dateBean)
                listener.getDateBean(dateBean)
                dismiss()
            }
        }

        // 注意：这里要给layout的parent设置peekHeight，而不是在layout里给layout本身设置，下面设置背景色同理，坑爹！！！
        val bottomSheetBehavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(
            bottomSheetView.parent as View
        )
        bottomSheetView.measure(0, 0)
        behavior.peekHeight = dp2px(486f)

        bottomSheetBehavior.peekHeight = dp2px(486f)
        //允许点击外部关闭，不允许滑动关闭
        bottomSheetBehavior.isHideable = false
        (bottomSheetView.parent as View).setBackgroundColor(context.resources.getColor(R.color.tram))
    }

    fun dp2px(dpValue: Float?): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dpValue!!, context.resources.displayMetrics
        ).toInt()
    }
}